﻿using System;
using System.Collections.Generic;
using System.Text;

namespace IupNET
{
    public class ComboList:EditBase
    {

        public ComboList()
            : base(Iupc.List(null))
        {
            
        }

        public ComboList(params string[] items)
            : base(Iupc.List(null))
        {
            int c = 1;
            foreach (string str in items)
            {
                SetAttribute(c.ToString(), str);
                c++;
            }
        }

        public void Add(string item)
        {
            SetAttribute("APPENDITEM", item);
        }
        

        public bool AutoHide
        {
            get
            {
                return GetAttribute("AUTOHIDE") == "YES";
            }
            set
            {
                SetAttribute("AUTOHIDE", value ? "YES" : "NO");
            }
        }

        public bool CanFocus
        {
            get
            {
                return GetAttribute("CANFOCUS") == "YES";
            }
            set
            {
                SetAttribute("CANFOCUS", value ? "YES" : "NO");
            }
        }

        public int Count
        {
            get
            {
                return Convert.StringToInt("COUNT");
            }
        }

        /// <summary>
        /// [Windows and GTK Only] (non inheritable): Enable or disable the drag-drop of files. 
        /// Default: false, but if DropFilesCB is defined when the element is mapped then it will 
        /// be automatically enabled.
        /// </summary>
        public bool DragDrop
        {
            get
            {
                return GetAttribute("DRAGDROP") == "YES";
            }
            set
            {
                SetAttribute("DRAGDROP", value ? "YES" : "NO");
            }
        }

        public bool DropDown
        {
            get
            {
                return GetAttribute("DROPDOWN") == "YES";
            }
            set
            {
                SetAttribute("DROPDOWN", value ? "YES" : "NO");
            }
        }

        public bool DropExpand
        {
            get
            {
                return GetAttribute("DROPEXPAND") == "YES";
            }
            set
            {
                SetAttribute("DROPEXPAND", value ? "YES" : "NO");
            }
        }

        public bool EditBox
        {
            get
            {
                return GetAttribute("EDITBOX") == "YES";
            }
            set
            {
                SetAttribute("EDITBOX", value ? "YES" : "NO");
            }
        }

        public void Insert(int pos, string value)
        {
            SetAttribute("INSERTITEM" + (pos + 1).ToString(),value);
        }

        public bool Multiple
        {
            get
            {
                return GetAttribute("MULTIPLE") == "YES";
            }
            set
            {
                SetAttribute("MULTIPLE", value ? "YES" : "NO");
            }
        }

        public void Remove(int pos)
        {
            SetAttribute("REMOVEITEM", (pos + 1).ToString());
        }

        public bool Scrollbar
        {
            get
            {
                return GetAttribute("SCROLLBAR") == "YES";
            }
            set
            {
                SetAttribute("SCROLLBAR", value ? "YES" : "NO");
            }
        }

        public bool ShowDropDown
        {
            set
            {
                SetAttribute("SHOWDROPDOWN", value ? "YES" : "NO");
            }
        }

        public int TopItem
        {
            set
            {
                SetAttribute("TOPITEM", value.ToString());
            }
        }

        public int Spacing
        {
            get
            {
                return Convert.StringToInt(GetAttribute("SPACING"));
            }
            set
            {
                SetAttribute("SPACING", value.ToString());
            }
        }

        public new string Value
        {
          //overridden for documentation purposes
          get
          {
            return GetAttribute("VALUE");
          }
          set
          {
            SetAttribute("VALUE", value);
          }
        }

        public int SelectedIndex
        {
            get
            {
                return Convert.StringToInt(Value)-1;
            }
            set
            {
                Value = (value+1).ToString();
            }
        }

        public int[] SelectedItems
        {
            get
            {
                string v = Value;
                int n = v.Length;
                List<int> res=new List<int>();
                for (int l = 0; l < n; l++)
                {
                    if (v[l] == '+')
                        res.Add(l);
                }
                return res.ToArray();
            }

            set
            {
                int n = Count;
                StringBuilder sb=new StringBuilder();
                for(int l=0;l<n;l++) {
                    if(Array.IndexOf(value,l)>=0)
                        sb.Append('+');
                    else
                        sb.Append('-');
                }

                Value = sb.ToString();
            }
        }


        public int VisibleItems
        {
            get
            {
                return Convert.StringToInt(GetAttribute("VISIBLE_ITEMS"));
            }
            set
            {
                SetAttribute("VISIBLE_ITEMS", value.ToString());
            }
        }

        public int VisibleColumns
        {
            get
            {
                return Convert.StringToInt(GetAttribute("VISIBLECOLUMNS"));
            }
            set
            {
                SetAttribute("VISIBLECOLUMNS", value.ToString());
            }
        }


        public int VisibleLines
        {
            get
            {
                return Convert.StringToInt(GetAttribute("VISIBLELINES"));
            }
            set
            {
                SetAttribute("VISIBLELINES", value.ToString());
            }
        }

        public int ConvertXYToPos(int x, int y)
        {
            return Iupc.ConvertXYToPos(cobject, x, y)-1; //we are zero based but iup api is 1 based
        }

        public EventCallback Action
        {
            get
            {
                return CBHandler.Fetch("ACTION_LIST");
            }
            set
            {
                CBHandler.Set("ACTION_LIST", value);
            }
        }


        public EventCallback DblClickCB
        {
            get
            {
                return CBHandler.Fetch("DBLCLICK_CB");
            }
            set
            {
                CBHandler.Set("DBLCLICK_CB", value);
            }
        }

        public EventCallback DropDownCB
        {
            get
            {
                return CBHandler.Fetch("DROPDOWN_CB");
            }
            set
            {
                CBHandler.Set("DROPDOWN_CB", value);
            }
        }

        public EventCallback EditCB
        {
            get
            {
                return CBHandler.Fetch("EDIT_CB");
            }
            set
            {
                CBHandler.Set("EDIT_CB", value);
            }
        }


        
        
    }
}
